package cn.schoolwow.quickdao.module.database.ddl.action.flow.table;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteUpdateConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class DropTableFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setDropTableStatement(flowContext);
        executeStatement(flowContext);
    }

    @Override
    public String name() {
        return "删除表";
    }

    private void setDropTableStatement(FlowContext flowContext){
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        String tableName = (String) flowContext.checkData("tableName");
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);

        switch (databaseType){
            case Postgresql:{
                flowContext.putTemporaryData("sql", "drop table " + quickDAOConfig.databaseContext.databaseProvider.escape(tableName) + " cascade;");
            }break;
            default:{
                flowContext.putTemporaryData("sql", "drop table " + quickDAOConfig.databaseContext.databaseProvider.escape(tableName) + ";");
            }break;
        }
    }

    private void executeStatement(FlowContext flowContext){
        flowContext.startFlow(new ExecuteUpdateConnectionFlow())
                .putTemporaryData("name", "删除表")
                .execute();
    }
}
